package demo.practice.leetcode_base;

import java.util.ArrayList;
import java.util.List;

public class P34 {
    public static void main(String[] args) {
        int [] arr= new int[]{2,2};
        P34 p34= new P34();
        p34.searchRange(arr,2);
    }


    public int[] searchRange(int[] nums, int target) {
        if(nums.length==0)
            return new int[]{-1,-1};
        return searchRange(nums,target,nums.length-1,0);
    }

    public int[] searchRange(int[] nums, int target,int max,int min){

        if(target==nums[min] )
            return searchRange(nums,target,min);

        if(target==nums[max] )
            return searchRange(nums,target,max);

        if(target<nums[min]|| target>nums[max]|| max-min<=1  )
            return new int[]{-1,-1};

        int middle= (max-min)/2 +min;
        if(nums[middle]>target){
            //target在middle的前面
            return  searchRange( nums, target,middle, min);

        }else if(nums[middle]<target){
            //target在middle的前面
            return  searchRange( nums, target,max, middle);
        }else{
            //相等   再找出附近相同值
            return searchRange(nums,target,middle);
        }
    }

    public int[] searchRange(int[] nums, int target,int i){

        int max=i;
        int min=i;
        for(int j=i-1;j>=0;j--){
            if(nums[j]!=nums[i]){
                min=j+1;
                break;
            }

            if(j==0)
                min=0;

        }

        for(int j=i+1;j<nums.length;j++){
            if(nums[j]!=nums[i] ){
                max=j-1;
                break;
            }

            if(j==nums.length-1)
                max=nums.length-1;
        }

        int [] rarr=new int[]{max,min};
        return rarr;
    }


}
